﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.ObjectLayer;
using Mimoza.Apps.Console.ViewModel;

namespace Mimoza.Apps.Console.Commands
{
    class MoveHostCommand: BaseConsoleCommand
    {
        HostsViewModel m_ViewModel = null;
        public MoveHostCommand(HostsViewModel viewModel) 
        {
            m_ViewModel = viewModel;
            m_ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(this.view_PropertyChanged);
        }

        void view_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (null == m_ViewModel) return;

            this.Visible = m_ViewModel.InFocus;
            if ( null != m_ViewModel.Context )
            {
                this.Enabled = true;
            }
            else 
            {
                this.Enabled = false;
            }
        }
        public override void Execute()
        {
            if (null == m_ViewModel) return;

            MoveHostCommandForm form = new MoveHostCommandForm();
            form.Init(m_ViewModel.Application);
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Mimoza.ObjectLayer.Container cont = form.m_SelectedContainer;
                foreach (object obj in (object[])m_ViewModel.Context)
                {
                    Mimoza.ObjectLayer.Host host = obj as Mimoza.ObjectLayer.Host;
                    host.Parent = cont;
                    m_ViewModel.Application.ObjectLayer.HostRepository.Update(host);
                }
                m_ViewModel.Application.ContainersTreeViewModel.Context = cont;
            }
        }
    }
}
